package com.example.demo.web.rest.controller;

import com.example.demo.client.SmQryClient;
import com.example.demo.client.evt.sysbasequery.*;
import com.example.demo.client.vm.sysbasequery.LogVm;
import com.example.demo.client.vm.sysbasequery.LoginLogVm;
import com.example.demo.client.vm.sysbasequery.OperateLogVm;
import com.ffcs.oss.common.utils.LocalMessageUtil;
import com.example.demo.enums.ExportListEnum;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.ExcelService;
import com.example.demo.service.LogService;
import com.example.demo.util.excel.ExcelCreateUtil;
import com.example.demo.web.rest.constant.ExportConstant;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.OperateLogEvt;
import com.example.demo.web.rest.evt.PeriodEvt;
import com.example.demo.web.rest.evt.log.MenuLogEvt;
import com.example.demo.web.rest.evt.log.QryOperateLogEvt;
import com.example.demo.web.rest.evt.log.QueryMenuLogEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 登录日志、操作日志、系统日志
 * @Author zhengmf
 * @Date 2018/12/26  18:28
 */
@Api("登录日志、操作日志、系统日志")
@RestController
@RequestMapping("/api/log")

public class LogController extends BaseController{
    @Autowired
    private LogService logService;
    @Autowired
    private SmQryClient smQryClient;

    @Autowired
    private ExcelService excelService;




    /***********************************登录日志********************************/
    @ApiOperation("查询登录日志")
    @PostMapping("/queryLoginLog")
    public ServiceResp<QueryPageVm<LoginLogVm>> queryLoginLog(@RequestBody LoginLogEvt evt){
        return baseQueryClient.queryLoginLog(evt);
    }

    @ApiOperation("删除指定时间段的登录日志")
    @PostMapping("/delLoginLogByPeriod")
    public ServiceResp<Integer> delLoginLogByPeriod(@RequestBody PeriodEvt evt){
        return ServiceResp.getInstance(Integer.class).success(logService.delLoginLogByPeriod(evt));
    }

    @ApiOperation("根据id删除登录日志")
    @PostMapping("/delLoginLogById")
    public ServiceResp<Integer> delLoginLogById(@RequestBody @Validated IdEvt evt){
        return ServiceResp.getInstance(Integer.class).success(logService.delLoginLogById(evt.getId()));
    }

    @ApiOperation("是否可以导出登录日志数据")
    @PostMapping("/isCanExportLoginLog")
    public ServiceResp<Boolean> isCanExportLoginLog(@RequestBody LoginLogEvt evt){
        return baseQueryClient.isCanExportLoginLog(evt);
    }

    @ApiOperation("登录日志导出")
    @GetMapping("/exportLoginLogOld")
    public void exportLoginLogOld(HttpServletResponse response, LoginLogEvt evt){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            List<LoginLogVm> dataList = baseQueryClient.queryLoginLog(evt).getBody().getRecords();
            String[] keyArray = {"loginName","loginIp","loginDesc","operateAction","createTime"};
            List<String> keys = Arrays.asList(keyArray);
            List<Map<String, Object>> list = ExcelCreateUtil.parseListMap(dataList, keys);
            String[] columnArray = {"用户名","IP地址","登录描述","操作类型","登录时间"};
            Workbook workbook = ExcelCreateUtil.createWorkBook(list, keys, Arrays.asList(columnArray),"sheet1");
            export(response,workbook,"登录日志");
        } catch (Exception e) {
            logger.error("登录日志导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    /***********************************系统日志********************************/
    @ApiOperation("查询系统日志")
    @PostMapping("/queryLog")
    public ServiceResp<QueryPageVm<LogVm>> queryLog(@RequestBody QueryLogEvt evt){
//        return baseQueryClient.queryLog(evt);
        return smQryClient.queryLog(evt);
    }

    @ApiOperation("是否可以导出系统日志数据")
    @PostMapping("/isCanExportLog")
    public ServiceResp<Boolean> isCanExportLog(@RequestBody QueryLogEvt evt){
        return baseQueryClient.isCanExportLog(evt);
    }

    @ApiOperation("删除指定时间段的系统日志")
    @PostMapping("/delLogByPeriod")
    public ServiceResp<Integer> delLogByPeriod(@RequestBody PeriodEvt evt){
        return ServiceResp.getInstance(Integer.class).success(logService.delLogByPeriod(evt));
    }

    @ApiOperation(" 根据id删除系统日志")
    @PostMapping("/delLogById")
    public ServiceResp<Boolean> delLogById(@RequestBody @Validated IdEvt evt){
        logService.delLogById(evt.getId());
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("系统日志导出")
    @GetMapping("/exportLog")
    public void exportLog(HttpServletResponse response, QueryLogEvt evt){
        try {
            List<LogVm> dataList = baseQueryClient.queryLog(evt).getBody().getRecords();
            String[] keyArray = {"userName","moduleDesc","operateDesc","operateResult","operateTime","endTime",
            "requestIp","times","requestUrl","remarks","input","output"};
            List<String> keys = Arrays.asList(keyArray);
            //将集合中对象的属性对应到List<Map<String,Object>>
            List<Map<String, Object>> list = ExcelCreateUtil.parseListMap(dataList, keys);
            String[] columnArray = {"操作人","模快名称","功能名称","操作结果","开始时间","结束时间",
                "请求IP","耗费时间","请求路径","说明","输入内容","输出内容"};
            Workbook workbook = ExcelCreateUtil.createWorkBook(list, keys, Arrays.asList(columnArray),"sheet1");
            export(response,workbook,"系统日志");
        } catch (Exception e) {
            logger.error("系统日志导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }



    /***********************************操作日志********************************/
    @ApiOperation("查询操作日志")
    @PostMapping("/queryOperateLog")
    public ServiceResp<QueryPageVm<OperateLogVm>> queryOperateLog(@RequestBody QueryOperateLogEvt evt){
//        return baseQueryClient.queryOperateLog(evt);
        return smQryClient.queryOperateLog(evt);
    }


    @ApiOperation("保存操作日志")
    @PostMapping("/saveOperateLog")
    public ServiceResp saveOperateLog(@RequestBody OperateLogEvt evt){
        logService.saveOperateLog(evt);
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("根据id删除操作日志")
    @PostMapping("/delOperateLogById")
    public ServiceResp delOperateLogById(@RequestBody @Validated IdEvt evt){
        logService.delOperateLogById(evt.getId());
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("是否可以导出操作日志数据")
    @PostMapping("/isCanExportOperateLog")
    public ServiceResp<Boolean> isCanExportOperateLog(@RequestBody QueryOperateLogEvt evt){
        return baseQueryClient.isCanExportOperateLog(evt);
    }

    @ApiOperation("操作日志导出")
    @PostMapping("/exportOperateLog")
    public void exportOperateLog(HttpServletResponse response, QueryOperateLogEvt evt){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            List<OperateLogVm> dataList = baseQueryClient.queryOperateLog(evt).getBody().getRecords();
            String[] keyArray = {"handleUserName","moduleType","operateAction","operateResultStatus","operateObjType",
                "operateTime", "operateComment","remarks"};
            List<String> keys = Arrays.asList(keyArray);
            List<Map<String, Object>> list = ExcelCreateUtil.parseListMap(dataList, keys);
            String[] columnArray ={"操作人","模块类型","操作动作","操作结果","操作对象类型","操作时间","操作内容","备注"};
            Workbook workbook = ExcelCreateUtil.createWorkBook(list, keys, Arrays.asList(columnArray),"sheet1");
            export(response,workbook,"操作日志");
        } catch (Exception e) {
            logger.error("登录日志导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("查询系统日志表")
    @PostMapping("/queryLogInfo")
    public ServiceResp queryLogInfo(@RequestBody QueryLogInfoEvt evt) throws Exception {
        //判断是否查询ES日志
        if (evt.getEslogselelct()) {
            return ServiceResp.getInstance(QueryPageVm.class).success(logService.queryLogInfoEs(evt));
        }
        return ServiceResp.getInstance(QueryPageVm.class).success(logService.queryLogInfo(evt));
    }

    @ApiOperation("获取日志查询下拉列表")
    @PostMapping("/queryLogSelect")
    public ServiceResp queryLogSelect() throws Exception {
        //判断是否查询ES日志
        return ServiceResp.getInstance(List.class).success(logService.queryLogSelect());
    }

    @ApiOperation("保存日志，菜单级")
    @PostMapping("/saveMenuLog")
    public ServiceResp saveMenuLog(@RequestBody MenuLogEvt evt, HttpServletRequest request){
        logService.saveMenuLog(evt, request);
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("查询日志，菜单级")
    @PostMapping("/queryMenuLog")
    public ServiceResp queryMenuLog(@RequestBody QueryMenuLogEvt evt){
        //判断是否查询ES日志
        return ServiceResp.getInstance(QueryPageVm.class).success(logService.queryMenuLog(evt));
    }

    @ApiOperation("根据区域进行统计")
    @PostMapping("/countByArea")
    public ServiceResp countByArea(@RequestBody QryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.countByArea(evt));
    }

    @ApiOperation("根据区域进行统计导出")
    @GetMapping("/exportOperateLogByArea")
    public void exportOperateLogByArea(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_AREA) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_BY_AREA.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_BY_AREA.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_AREA),ExportListEnum.EXPORT_OPERATE_LOG_BY_AREA.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("根据单位进行统计")
    @PostMapping("/countByCompany")
    public ServiceResp countByCompany(@RequestBody QryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.countByCompany(evt));
    }

    @ApiOperation("根据单位进行统计导出")
    @GetMapping("/exportOperateLogByCompany")
    public void exportOperateLogByCompany(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_COMPANY) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_BY_COMPANY.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_BY_COMPANY.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_COMPANY),ExportListEnum.EXPORT_OPERATE_LOG_BY_COMPANY.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("根据用户进行统计")
    @PostMapping("/countByUser")
    public ServiceResp countByUser(@RequestBody QryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.countByUser(evt));
    }

    @ApiOperation("根据用户进行统计导出")
    @GetMapping("/exportOperateLogByUser")
    public void exportOperateLogByUser(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_USER) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_BY_USER.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_BY_USER.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_USER),ExportListEnum.EXPORT_OPERATE_LOG_BY_USER.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("根据功能进行统计")
    @PostMapping("/countByFun")
    public ServiceResp countByFun(@RequestBody QryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.countByFun(evt));
    }

    @ApiOperation("根据功能进行统计导出")
    @GetMapping("/exportOperateLogByFun")
    public void exportOperateLogByFun(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_FUN) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_BY_FUN.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_BY_FUN.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_FUN),ExportListEnum.EXPORT_OPERATE_LOG_BY_FUN.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("根据忙时进行统计")
    @PostMapping("/countByBusy")
    public ServiceResp countByBusy(@RequestBody QryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.countByBusy(evt));
    }

    @ApiOperation("根据忙时进行统计导出")
    @GetMapping("/exportOperateLogByBusy")
    public void exportOperateLogByBusy(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_BUSY) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_BY_BUSY.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_BY_BUSY.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_BY_BUSY),ExportListEnum.EXPORT_OPERATE_LOG_BY_BUSY.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("获取操作日志列表")
    @PostMapping("/listOperateLog")
    public ServiceResp listOperateLog(@RequestBody com.example.demo.web.rest.evt.log.QueryOperateLogEvt evt){
        return ServiceResp.getInstance().success(logService.listOperateLog(evt));
    }

    @ApiOperation("导出操作日志列表")
    @GetMapping("/exportOperateLog")
    public void exportOperateLog(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_LIST) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_OPERATE_LOG_LIST.getExpColEn(),ExportListEnum.EXPORT_OPERATE_LOG_LIST.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_OPERATE_LOG_LIST),ExportListEnum.EXPORT_OPERATE_LOG_LIST.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("根据区域进行统计导出异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation("获取登录日志列表")
    @PostMapping("/listLoginLog")
    public ServiceResp listLoginLog(@RequestBody com.example.demo.web.rest.evt.log.LoginLogEvt evt){
        return ServiceResp.getInstance().success(logService.listLoginLog(evt));
    }

    @ApiOperation("导出登录日志列表")
    @GetMapping("/exportLoginLog")
    public void exportLoginLog(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "paramStr") String paramStr){
        try {
            //将集合中对象的属性对应到List<Map<String,Object>>
            String exportFileName = LocalMessageUtil.getMessage(ExportConstant.EXPORT_LOGIN_LOG_LIST) + ExportConstant.EXPORT_FILE_FORMAT;
            excelService.exportExcel(request,response, ExportListEnum.EXPORT_LOGIN_LOG_LIST.getExpColEn(),ExportListEnum.EXPORT_LOGIN_LOG_LIST.getExpColCn(),
                    exportFileName,LocalMessageUtil.getMessage(ExportConstant.EXPORT_LOGIN_LOG_LIST),ExportListEnum.EXPORT_LOGIN_LOG_LIST.getValue(), paramStr);
        } catch (Exception e) {
            logger.error("导出登录日志列表异常：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
